﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

namespace DownloadInterface
{
    public class Serializer
    {
        private static string DM_FILE_NAME = "dm.bin";
        private static string FM_FILE_NAME = "fm.bin";
        public static void SerializeDowloadManager(string filename, DownloadManager manager)
        {
            using (Stream s = File.Create(filename))
            {
                BinaryFormatter bf = new BinaryFormatter();
                
                bf.Serialize(s,manager);
            }
        }

        public static DownloadManager DeSerializeDownloadManager(string filename)
        {
            if (!File.Exists(filename))
            {
                return new DownloadManager();
            }
            using (Stream s = File.Open(filename,FileMode.Open))
            {
                BinaryFormatter bf = new BinaryFormatter();
                return (DownloadManager)bf.Deserialize(s);
            }
        }

        public static void SerializeFileManager(string filename, FileManage manager)
        {
            using (Stream s = File.Create(filename))
            {
                BinaryFormatter bf = new BinaryFormatter();
                bf.Serialize(s, manager);
            }
        }

        public static FileManage DeSerializeFileManager(string filename)
        {
            if (!File.Exists(filename))
            {
                return new FileManage();
            }
            using (Stream s = File.Open(filename, FileMode.Open))
            {
                BinaryFormatter bf = new BinaryFormatter();
                return (FileManage)bf.Deserialize(s);
            }
        }
        
        public static void SerializeAllObject(DownloadManager downloadManager, FileManage fileManager)
        {
            SerializeDowloadManager(DM_FILE_NAME, downloadManager);
            SerializeFileManager(FM_FILE_NAME, fileManager);
        }
    }
}
